<template>
    <nw-PageTitle :title="$t('system.versionInfo')" isBack />
    <nw-PageContainer>
        <view class="content">
            <view class="title" style="margin-top: 100px;">插件调试</view>
            <view style="margin-top: 40rpx;">网络服务测试</view>
            <button class="btn" @click="startNetworkServer">启动UDP、TCP服务</button>
            <button class="btn" @click="sendDeviceMsg">发送消息</button>
            <button class="btn" @click="stopNetworkServer">停止UDP、TCP服务</button>
            
<!--            <view style="margin-top: 40rpx;">UDP测试</view>
            <button class="btn" @click="startUdp">启动UDP</button>
            <button class="btn" @click="sendUdpMsg">发送UDP消息</button>
            <button class="btn" @click="stopUdp">停止UDP</button>
            <view style="margin-top: 40rpx;">TCPServer测试</view>
            <button class="btn" @click="startTcpServer">启动TCPServer</button>
            <button class="btn" @click="sendTcpServerMsg">发送TCP消息</button>
            <button class="btn" @click="closeTcpClient">断开TCP客户端</button>
            <button class="btn" @click="stopTcpServer">停止TCPServer</button> -->
        </view>
        <view>
            <view>UDP客户端</view>
            <view v-for="(item, index) in deviceOfBroadcast" :key="item.sn">
                {{item.sn}} - {{item.ip}} - {{item.port}}
            </view>

            <view>TCP客户端</view>
            <view v-for="(item, index) in dviceOfOnline" :key="item.sn">
                设备标识：{{item.sn}}
                设备会话ID：{{item.sessionId}}
                设备IP：{{item.ip}}
                设备端口：{{item.port}}
            </view>
        </view>
    </nw-PageContainer>
</template>

<script setup>
    const props = defineProps({
        title: {
            type: String,
            default: ''
        }
    })

    const loading = ref(true)
    const tableList = ref([])

    onMounted(_ => {})

    import {
        MsgType,
        DeviceMessageSend,
        DeviceMessageReceive
    } from '@/utils/deviceProtocolUtil.js'
    
    import { deviceProtocolService } from '@/service/deviceProtocolService.js'
    
    let deviceOfBroadcast = deviceProtocolService.getBroadcastDeviceList()
    let dviceOfOnline = deviceProtocolService.getOnlineDeviceList()

    let startNetworkServer = function() {
        deviceProtocolService.setupNetworkServer()
    }
    let sendDeviceMsg = function() {
        // deviceProtocolService.sendRecipe("N21250523152535", {})
        // deviceProtocolService.startCooking("N21250523152535", "R20250620140708")
        // deviceProtocolService.pauseCooking("N21250523152535")
        // deviceProtocolService.resumeCooking("N21250523152535")
        deviceProtocolService.manualAddSeasoning("N21250523152535", [])
        .then(r => {
            console.log("发送消息成功：", r)
        })
        .catch(e => {
            console.log("发送消息失败：", e)
        })
    }
    let stopNetworkServer = function() {
        deviceProtocolService.destroyNetworkServer()
    }

    ///////////////////// UDP插件，支持服务端，以及客户端链接 ///////////////
    const useDeviceDiscoverStore = useOutsideDeviceDiscoverStore()
    let deviceList = useDeviceDiscoverStore.getDeviceSessionList()

    let startUdp = function() {
        useDeviceDiscoverStore.startUdpServer()
    }
    let sendUdpMsg = function() {}

    let stopUdp = function() {
        useDeviceDiscoverStore.stopUdpServer()
    }

    ///////////////////// TCP server ///////////////
    const tcpServer = uni.requireNativePlugin('tcp-server');
    const useDeviceProtocolStore = useOutsideDeviceProtocolStore()
    let deviceTcpList = useDeviceProtocolStore.getDeviceSessionList()

    let startTcpServer = function() {
        console.log("启动TCP Server: ");
        useDeviceProtocolStore.startTcpServer()
    }
    let sendTcpServerMsg = function() {
        let msg = new DeviceMessageSend(MsgType.RecipeSend, [])
        useDeviceProtocolStore.sendMessage("N20250523152535", msg)
            .then((data) => {
                console.log("发送成功:", data);
            })
            .catch((error) => {
                console.error("发送失败:", error);
            });
    }
    let closeTcpClient = function() {

    }
    let stopTcpServer = function() {
        console.log("停止TCP Server: ");
        useDeviceProtocolStore.stopTcpServer()
    }
</script>

<style lang="scss" scoped>
    .content {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 100%;

        .title {
            font-size: 20 * 2rpx;
            font-weight: bold;
            color: var(--color-333);
        }

        .btn {
            margin-top: 10px;
            width: 90%;
        }
    }
</style>